/*
 * copyright (c) 2009 Alexandr [Sn@ble] Surnin
 *
 * This file is part of FrameSeeker.
 *
 * FrameSeeker is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FrameSeeker is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You can read the full version of GNU LGPL in
 * http://www.gnu.org/copyleft/lesser.html
 */


#include "fsconfigurator.h"
#include "ui_fsconfigurator.h"

FSConfigurator::FSConfigurator(QWidget *parent)
    : QWidget(parent), ui(new Ui::FSConfigurator) {

    ui->setupUi(this);

    sbLow = qFindChild<QSpinBox*>(this, "sbLowThres");
    sbHigh = qFindChild<QSpinBox*>(this, "sbHighThres");

    settings = new QSettings ("frameSeeker.conf", QSettings::IniFormat);
    settings->setIniCodec("UTF-8");

    // Load previous settings
    on_pbCancel_clicked();
}

FSConfigurator::~FSConfigurator() {

    delete settings;
    delete ui;
}

void FSConfigurator::on_pbExit_clicked() {

    this->close();
}

void FSConfigurator::saveSettings() {

    settings->beginGroup("Image");
        settings->setValue("Save", ui->cbSaveImage->isChecked());
        settings->setValue("Format", ui->cbFormat->currentText());
        settings->setValue("Quality", ui->sbQuality->value());
        if (ui->cbScaleToWidth->isChecked()) {
            settings->setValue("Width", ui->sbScaleToWidth->value());
        } else {
            settings->setValue("Width", 0);
        }
    settings->endGroup();

    settings->beginGroup("Method");
        settings->setValue("Name", ui->cbMethod->currentText());
        settings->setValue("Testing_mode", ui->cbSavePrev->isChecked());
    settings->endGroup();

    settings->beginGroup("Motion_Vectors_Method");
        // TODO
        //settings->setValue("Save", QString("true"));
    settings->endGroup();

    settings->beginGroup("Histograms_Method");
        settings->setValue("Low_threshold", ui->sbLowThres->value());
        settings->setValue("High_threshold", ui->sbHighThres->value());
    settings->endGroup();
}

void FSConfigurator::loadSettings() {

    // Image section
    ui->cbSaveImage->setChecked(settings->value("Image/Save").toBool());
    ui->cbFormat->setCurrentIndex(ui->cbFormat->findText(settings->value("Image/Format").toString()));
    ui->sbQuality->setValue(settings->value("Image/Quality").toInt());

    int stw = settings->value("Image/Width").toInt();
    if (stw == 0) {
        ui->cbScaleToWidth->setChecked(false);
    } else {
        ui->cbScaleToWidth->setChecked(true);
        ui->sbScaleToWidth->setValue(stw);
    }


    // Method section
    ui->cbMethod->setCurrentIndex(ui->cbMethod->findText(settings->value("Method/Name").toString()));
    ui->cbSavePrev->setChecked(settings->value("Method/Testing_mode").toBool());

    // Motion Vectors method section
    // TODO

    // Histograms method section
    ui->sbLowThres->setValue(settings->value("Histograms_Method/Low_threshold").toInt());
    ui->sbHighThres->setValue(settings->value("Histograms_Method/High_threshold").toInt());

}

void FSConfigurator::on_pbSave_clicked() {

    saveSettings();
}

void FSConfigurator::on_pbCancel_clicked() {

    loadSettings();
}

void FSConfigurator::on_sbLowThres_valueChanged(int v) {

    if (sbHigh->value() > v) {
        sbLow->setValue(v);
    } else {
        sbLow->setValue(sbHigh->value());
    }
}

void FSConfigurator::on_sbHighThres_valueChanged(int v) {

    if (sbLow->value() < v) {
        sbHigh->setValue(v);
    } else {
        sbHigh->setValue(sbLow->value());
    }
}
